package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import cn.b.sky.mes.mm.dao.SafetyinventroyDao;
import cn.b.sky.mes.mm.service.SafetyinventroyService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.Safetyinventroy;
import cn.b.sky.mes.mm.model.condition.SafetyinventroyCondition;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.user.SecurityContextUtil;

@Service("safetyinventroyService")
public class SafetyinventroyServiceImpl implements SafetyinventroyService {

  private static final Logger LOG = LoggerFactory.getLogger(SafetyinventroyServiceImpl.class);

  @Autowired
  private SafetyinventroyDao safetyinventroyDao;
  
  @Override
  public List<Safetyinventroy> getSafetyinventroysByCondition(SafetyinventroyCondition condition) {
    if (condition == null) {
      return null;
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("modifyDatetime");//修改时间排序
    }
    return safetyinventroyDao.selectSafetyinventroyByCondition(condition);
  }

  @Override
  public int getSafetyinventroyCountByCondition(SafetyinventroyCondition condition) {
    if (condition == null) {
      return 0;
    }
    return safetyinventroyDao.selectSafetyinventroyCountByCondition(condition);
  }

  @Override
  public Safetyinventroy getSafetyinventroyByPk(String siteCode, Long id) {
    if (null == id || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return safetyinventroyDao.selectSafetyinventroyById(id, siteCode);
  }

  @Override
  public void saveSafetyinventroy(Safetyinventroy safetyinventroy) {
    if (safetyinventroy != null && !StringUtils.isBlank(safetyinventroy.getSiteCode())) {
      safetyinventroyDao.insertSafetyinventroy(safetyinventroy);
    }

  }

  @Override
  public void modifySafetyinventroy(Safetyinventroy safetyinventroy) {
    if (safetyinventroy != null && !StringUtils.isBlank(safetyinventroy.getSiteCode())) {
      safetyinventroyDao.updateSafetyinventroy(safetyinventroy);
    }
  }

  @Override
  public String deleteSafetyinventroyBySiteCodeAndId(String siteCode, Long id) {
    String message = "";
    if (StringUtils.isBlank(siteCode) || null == id) {
      return null;
    }
    try {
      safetyinventroyDao.deleteSafetyinventroyById(id, siteCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        // 外键删除已存在提示
        message = SystemConstant.DATABASE_OPERATION_FK;
      } else {
        message = SystemConstant.DWZ_CODE_ERROR_MESSAGE;
      }
    }
    return message;
  }

  @Override
  public boolean isSetStorageLocationAndWarehouseCode(Safetyinventroy safetyinventroy) {
    if (StringUtils.isBlank(safetyinventroy.getMaterialCode())) {
      return false;
    }
    SafetyinventroyCondition condition = new SafetyinventroyCondition();
    condition.setMaterialCode(safetyinventroy.getMaterialCode());
    condition.setSiteCode(safetyinventroy.getSiteCode());
    condition.setWarehouseCode(safetyinventroy.getWarehouseCode());
    //先根据库存安全级别查询是否已存在
    List<Safetyinventroy> safetyinventroys = safetyinventroyDao.selectSafetyinventroyByCondition(condition);
    if (StringUtils.isBlank(safetyinventroy.getStorageLocationCode())) {
      if (safetyinventroys != null && safetyinventroys.size() > 0) {
        return false;
      }
    } else {
      //存在库位级别的先判断此物料是否存在同仓库安全级别 ，存在继续判断是否存在同库位安全级别
      if (safetyinventroys != null && safetyinventroys.size() > 0) {
        condition.setStorageLocationCode(safetyinventroy.getStorageLocationCode());
        List<Safetyinventroy> safetyinventroyss = safetyinventroyDao.selectSafetyinventroyByCondition(condition);
        //
        if (safetyinventroyss != null && safetyinventroyss.size() > 0) {
          return false;
        }
      }

    }
    return true;
  }

  @Override
  public Safetyinventroy getSafetyInventory(SafetyinventroyCondition condition) {
    // 如果没有物料或者没有仓库
    if (StringUtils.isEmpty(condition.getMaterialCode()) || StringUtils.isEmpty(condition.getWarehouseCode())) {
      return null;
    }
    if (StringUtils.isEmpty(condition.getSiteCode())) {
      condition.setSiteCode(SecurityContextUtil.getSiteCode());
    }
    return safetyinventroyDao.getSafetyInventory(condition);
  }

}
